<template>
    <div class="layout">
        <Layout>
            <Header>
                <Menu mode="horizontal" theme="dark" active-name="1">
                    <div class="layout-logo">后台管理系统</div>
                    <div class="layout-nav">
                        <Submenu name="1" class="nav-menu nav-menu-item">
                            <template slot="title">
                                <Icon type="md-contact" />
                                欢迎，{{ username }}
                            </template>
                            <MenuItem name="1-1">网站信息</MenuItem>
                            <MenuItem name="1-2">个人信息</MenuItem>
                            <MenuItem name="1-3">密码修改</MenuItem>
                            <MenuItem name="1-4"><div @click="logout">退出</div></MenuItem>
                        </Submenu>
                        <MenuItem name="2" class="nav-menu nav-menu-icon">
                            <Icon type="ios-notifications-outline" size="20" />
                        </MenuItem>
                        <MenuItem name="3" class="nav-menu nav-menu-icon">
                            <Icon type="md-refresh" size="20" />
                        </MenuItem>
                    </div>
                </Menu>
            </Header>
            <Layout>
                <Sider hide-trigger :style="{background: '#fff'}">
                    <Menu active-name="1-2" theme="light" width="auto" :open-names="['1']">
                        <Submenu name="1">
                            <template slot="title">
                                <Icon type="ios-navigate"></Icon>
                                文章管理
                            </template>
                            <router-link to="/index/article_list">
                                <MenuItem name="1-1">文章列表</MenuItem>
                            </router-link>
                            <router-link to="/index/add_category">
                                <MenuItem name="1-2">添加栏目</MenuItem>
                            </router-link>
                            <router-link to="/index/article_column">
                                <MenuItem name="1-3">栏目列表</MenuItem>
                            </router-link>
                        </Submenu>
                    </Menu>
                </Sider>
                <Layout :style="{padding: '0 24px 24px'}">
                    <Breadcrumb :style="{margin: '24px 0'}">
                        <BreadcrumbItem to="/index">
                            Home
                        </BreadcrumbItem>
                        <BreadcrumbItem>{{second_bread}}</BreadcrumbItem>
                    </Breadcrumb>
                    <Content :style="{padding: '24px', minHeight: '420px', background: '#fff'}">
                        <router-view></router-view>
                    </Content>
                </Layout>
            </Layout>
        </Layout>
    </div>
</template>

<script>
    export default {
        name: 'Index',
        data() {
            return {
                second_bread: this.$route.name,
                username: ''
            }
        },
        methods: {
            getBreadcrumb() {
                console.log('路由发生了变化')
                this.second_bread = this.$route.name
            },
            // 退出登录
            logout() {
                localStorage.setItem('jwt', '');
                localStorage.setItem('admin', '');
                this.$router.push({path: '/login'});
            }
        },
        // 监听路由变化
        watch: {
            $route() {
                this.getBreadcrumb();
            }
        },
        mounted: function () {
            var admin = localStorage.getItem('admin');
            if (admin) {
                // JSON.parse() 方法用于将一个 JSON 字符串转换为对象。
                admin = JSON.parse(admin);
                this.username = admin.name;
            }
        }
    }
</script>

<style scoped>
    .layout {
        border: 1px solid #d7dde4;
        background: #f5f7f9;
        position: relative;
        border-radius: 4px;
        overflow: hidden;
    }

    .layout-logo {
        border-radius: 3px;
        float: left;
        position: relative;
        left: 20px;
        color: #fff;
        text-align: center;
    }

    .layout-nav {
        width: 420px;
        margin: 0 auto;
        margin-right: 20px;
    }

    .layout-nav .nav-menu {
        padding: 0 10px;
        float: right;
    }
</style>
